package com.yupfeg.executor

import java.util.concurrent.ExecutorService

/**
 * 提供线程池实例的工厂类
 * @author yuPFeG
 * @date 2022/01/16
 */
interface ExecutorFactory {
    /**
     * 创建IO密集型线程池
     * - 通常用于占用时间短的任务，实际并发量很小的任务
     * */
    fun createIOThreadPool() : ExecutorService

    /**
     * 创建CPU密集型线程池
     * - 通常用于线程占用时间较长的任务，需要控制并发量
     * */
    fun createCPUThreadPool() : ExecutorService
}

/**
 * 默认实现的线程池工厂类
 * */
internal class DefaultExecutorFactory : ExecutorFactory {

    override fun createIOThreadPool(): ExecutorService {
        return ThreadPoolBuilder.createDefaultIOBuilder().build()
    }

    override fun createCPUThreadPool(): ExecutorService {
        return ThreadPoolBuilder.createDefaultCpuBuilder().build()
    }

}